package com.maotouying.owlsmart.youngLadyPage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.maotouying.owlsmart.R;
import com.maotouying.owlsmart.SmartUtil.NoScrollGridView;
import com.maotouying.owlsmart.SmartUtil.SystemUtil;
import com.maotouying.owlsmart.homePage.homeActivity;
import com.maotouying.owlsmart.homePage.younglady_A;
import com.maotouying.owlsmart.homePage.younglady_C;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class youngladyListActivity extends AppCompatActivity {

    private List<younglady_C> younglady_cs= new ArrayList<younglady_C>();
    private younglady_C younglady_c;
    private younglady_A younglady_a;

    private ImageView iv_back;
    private NoScrollGridView lv_youngladylist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_younglady_list);

        //顶部框颜色
        SystemUtil.setStatusBarColor(this, this.getResources().getColor(R.color.top_background_color));

        //返回按钮
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //视图
        lv_youngladylist =(NoScrollGridView)findViewById(R.id.lv_youngladylist);


        Intent intent=getIntent();
        String homeSearch=intent.getStringExtra("search");
        okhttp_text(homeSearch);
    }



    public void okhttp_text(String search) {


        //创建okHttpclient的对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建Request,Builder
        Request.Builder builder = new Request.Builder();
        //使用Request.Builder对象,调用Url方法,传入网路路径
        Request.Builder url = builder.url("http://103.45.145.67/testfolder/ylsearch.php?content="+search);
        //使用Request.Builder对象,调用builder方法构件request对象
        Request request = url.build();
        //创建一个Call对象,参数就request对象
        Call call = okHttpClient.newCall(request);
        //使用call对象,调用enqueue方法,请求加入调度(异步加载)

        call.enqueue(new Callback() {
            //当请求失败的时候,调用此方法
            @Override
            public void onFailure(Call call, IOException e) {
                //andler.sendEmptyMessage(FALL);
            }

            //当请求成功的时候,调用此方法
            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String source = response.body().string();
                System.out.println(source);

                // vod_id,vod_name,vod_class,vod_year,vod_pic
                //开始循环遍历以上字段
//list_c

                List<younglady_C> ls = JSON.parseArray(source, younglady_C.class);
                for (younglady_C pp : ls) {
                    younglady_c = new younglady_C();
                    //mty_id,mty_province,mty_date_add,mty_feature,mty_business,mty_price,mty_photos;
                    String mty_id = pp.getMty_id();
                    String mty_province = pp.getMty_province();
                    String mty_date_add = pp.getMty_date_add();
                    String mty_feature = pp.getMty_feature();
                    String mty_business = pp.getMty_business();
                    String mty_price = pp.getMty_price();
                    String mty_photos = pp.getMty_photos();

                    String photourl[] =  mty_photos.split(",");


                    younglady_c.setMty_id(mty_id);
                    younglady_c.setMty_province(mty_province);
                    younglady_c.setMty_date_add("更新:"+mty_date_add);
                    younglady_c.setMty_feature(mty_feature);
                    younglady_c.setMty_business("时间:"+mty_business);
                    younglady_c.setMty_price("价格一览:"+mty_price);
                    younglady_c.setMty_photos("http://103.45.145.67/testsex/ps/"+photourl[0]);


                    younglady_cs.add(younglady_c);

                }
                Message msg = new Message();
                msg.what = 21;
                Thandler.sendMessage(msg);//用activity中的handler发送消息


            }
        });

    }
    private Handler Thandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 21:
                    clickData((ArrayList<younglady_C>) younglady_cs);
                    break;

            }
        }
    };
    public void clickData(ArrayList<younglady_C> younglady_cs) {

        younglady_a = new younglady_A(R.layout.item_younglady, youngladyListActivity.this, younglady_cs);


        lv_youngladylist.setAdapter(younglady_a);
        lv_youngladylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // Toast.makeText(ChartActivity.this, "Click item" + i, Toast.LENGTH_SHORT).show();
                final TextView mty_id = (TextView) view.findViewById(R.id.mty_id);//id

                System.out.println();

//                // 带参跳转
                Intent intent=new Intent();
                intent.putExtra("Activity","homeActivity");//传一个页面过去
                intent.putExtra("mty_id", mty_id.getText().toString());//传一个id过去
                intent.setClass(youngladyListActivity.this, youngladyDetailActivity.class);//从哪里跳到哪里
                youngladyListActivity.this.startActivity(intent);


            }
        });


    }


}